package com.zygk.library.util.imagePicker;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.zygk.library.R;
import com.zygk.library.util.ImageManager;
import com.zygk.library.util.StringUtils;
import java.util.List;

/**
 * Created by admin on 2016/6/13.
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

  private LayoutInflater mInflater;
  private List<String> mDatas;
  private ImageManager imageManager;
  private Context mContext;
  private Activity mActivity;
  private int selectedId, itemResID;

  public ImageAdapter(Context context, List<String> datats) {
    mInflater = LayoutInflater.from(context);
    mContext = context;
    mActivity = (Activity) context;
    mDatas = datats;
    imageManager = new ImageManager(context);
  }
  public ImageAdapter(Context context, List<String> datats, int itemResID) {
    mInflater = LayoutInflater.from(context);
    mContext = context;
    mActivity = (Activity) context;
    mDatas = datats;
    imageManager = new ImageManager(context);
    this.itemResID = itemResID;
  }

  public static class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View arg0) {
      super(arg0);
    }

    ImageView iv;
  }

  @Override public int getItemCount() {
    return mDatas.size();
  }

  /**
   * 创建ViewHolder
   */
  @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view;
    if(itemResID > 0){
      view = mInflater.inflate(itemResID, viewGroup, false);
    }else {
      view = mInflater.inflate(R.layout.library_list_item_image, viewGroup, false);
    }
    ViewHolder viewHolder = new ViewHolder(view);
    viewHolder.iv = (ImageView) view.findViewById(R.id.iv_img);

    return viewHolder;
  }

  /**
   * 设置值
   */
  @Override public void onBindViewHolder(ViewHolder holder, final int position) {

    String path = mDatas.get(position);
    if (!StringUtils.isBlank(path)) imageManager.loadUrlImage(path, holder.iv);

    holder.iv.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (mOnItemClickLitener != null) {
          mOnItemClickLitener.onItemClick(position);
        }
      }
    });
  }

  /**
   * ItemClick的回调接口
   */
  public interface OnItemClickLitener {
    void onItemClick(int pos);
  }

  private OnItemClickLitener mOnItemClickLitener;

  public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
    this.mOnItemClickLitener = mOnItemClickLitener;
  }
}
